package zhongbo.java.air.flightpath.listener;

import cn.hutool.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;
import zhongbo.java.air.flightpath.entity.AirFlightPath;
import zhongbo.java.air.flightpath.mapper.AirFlightPathMapper;

import java.util.Date;
import java.util.UUID;

@Component
public class RedisSubscribeLogChanelListener implements MessageListener {
    @Autowired
    private AirFlightPathMapper airFlightPathMapper;
    @Autowired
    private RedisLogChanelPublisher redisLogChanelPublisher;

    @Override
    public void onMessage(Message message, byte[] pattern) {

        String msg = new String(message.getBody());
        System.out.println("收到消息：" + msg);
        AddAirFlightPath(msg);
    }

    //icao_code=acb84d, timestamp=null, latitude=61.172218, longitude=-149.999183, altitude=0.0, speed=26.5, heading=345.9, source_type=null)
    private void AddAirFlightPath(String msg) {
        JSONObject jsonObject = new JSONObject(msg);
        AirFlightPath airFlightPath = new AirFlightPath();
        String IcaoCode = jsonObject.getStr("icaoCode");
        Date timestamp = jsonObject.getDate("timestamp");
        Double latitude = jsonObject.getDouble("latitude");
        Double longitude = jsonObject.getDouble("longitude");
        Double altitude = jsonObject.getDouble("altitude");
        Double speed = jsonObject.getDouble("speed");
        Double heading = jsonObject.getDouble("heading");
        String source_type = jsonObject.getStr("sourceType");
        if (IcaoCode == null) {
            airFlightPath.setIcaoCode("");
        } else {
            airFlightPath.setIcaoCode(IcaoCode);
        }
        if (latitude == null) {
            airFlightPath.setLatitude(0.0);
        } else {
            airFlightPath.setLatitude(latitude);
        }
        if (longitude == null) {
            airFlightPath.setLongitude(0.0);
        } else {
            airFlightPath.setLongitude(longitude);
        }
        if (altitude == null) {
            airFlightPath.setAltitude(0);
        } else {

            airFlightPath.setAltitude(altitude.intValue());
        }
        if (speed == null) {
            airFlightPath.setSpeed(0);
        } else {
            airFlightPath.setSpeed(speed.intValue());
        }
        if (heading == null) {
            airFlightPath.setHeading(0);
        } else {
            airFlightPath.setHeading(heading.intValue());
        }
        if (source_type == null) {
            airFlightPath.setSourceType("");
        } else {
            airFlightPath.setSourceType(source_type);
        }
        airFlightPath.setTimestamp(timestamp);
        airFlightPath.setId(UUID.randomUUID().toString().substring(0, 8));
        int ok = airFlightPathMapper.insert(airFlightPath);
        if (ok > 0) {
            System.out.println("数据插入成功");
         redisLogChanelPublisher.broadcastJson(airFlightPath);
        }


    }
}
